| 1 | 1 |  | var axios = require('axios'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | var PKIType = require('./x509/pkitype'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 | 1 |  | var ProtoBuf = require('./protobuf'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 | 1 |  | var PaymentRequest = ProtoBuf.PaymentRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | function checkContentType(response, expecting) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 2 |  |     if (!("content-type" in response.headers)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |         throw new Error("Missing content-type header in response"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 2 |  |     if (response.headers["content-type"] !== expecting) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         throw new Error("Invalid content-type header set by server, request failed"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  | var HttpClient = function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @param {string} url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @param {Validator} validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @param {NetworkConfig} networkConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @return Promise of PaymentRequest | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 1 |  | HttpClient.prototype.getRequest = function(url, validator, networkConfig) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     return axios({ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         method: 'get', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         headers: { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             "Accept": networkConfig.getMimeTypes().PAYMENT_REQUEST | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         }, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         url: url, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         responseType: 'arraybuffer' | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     }) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         .then(function(response) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             checkContentType(response, networkConfig.getMimeTypes().PAYMENT_REQUEST); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             var buf = Buffer.from(response.data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             var paymentRequest = PaymentRequest.decode(buf); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             var path = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 2 |  |             if (paymentRequest.pkiType !== PKIType.NONE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                 path = validator.verifyX509Details(paymentRequest); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             return [paymentRequest, path]; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * @param {ProtoBuf.PaymentDetails} details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  * @param {string[]} txs - list of transactions fulfilling request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  * @param {string|null} memo - optional memo for merchant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  * @returns {{transactions: Array}} - Payment object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  | HttpClient.prototype.preparePayment = function(details, txs, memo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     var detailKeys = Object.keys(details); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |     if (detailKeys.indexOf("paymentUrl") === -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         throw new Error("Not payment url on details"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     var payment = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         transactions: [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     txs.map(function(tx) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         if (typeof tx === "string") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             payment.transactions.push(tx); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             throw new Error("txs must be a list of raw transactions"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |     if (detailKeys.indexOf("merchantData") !== -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         payment.merchantData = details.merchantData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |     if (typeof memo === "string") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         payment.memo = details.memo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     return payment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  * @param {ProtoBuf.PaymentDetails} details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  * @param {string[]} txs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  * @param {string|null} memo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  * @param {NetworkConfig} networkConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  * @returns Promise of PaymentACK | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  | HttpClient.prototype.sendPayment = function(details, txs, memo, networkConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     var payment = this.preparePayment(details, txs, memo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     var paymentData = ProtoBuf.Payment.encode(payment).final(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     return axios({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         method: 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         headers: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             "Accept": networkConfig.getMimeTypes().PAYMENT_ACK, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             "Content-Type": networkConfig.getMimeTypes().PAYMENT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         url: details.paymentUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         data: paymentData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         responseType: 'arraybuffer' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         .then(function(response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             checkContentType(response, networkConfig.getMimeTypes().PAYMENT_ACK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             var buf = Buffer.from(response.data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return ProtoBuf.PaymentACK.decode(buf); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | module.exports = HttpClient; | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.